//
//  WBRetweetView.swift
//  WeiBo
//
//  Created by chenguanglei on 16/11/29.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit


class WBRetweetView: UIView {
    
    var bottom: Constraint?
    
    var retweetViewModel: WBHomeViewModel? {
        
        didSet{
            
            
            contentLabel.text = retweetViewModel?.status?.retweeted_status?.text
            
            //没有转发内容,隐藏转发视图
//            self.isHidden = contentLabel.text==nil ? true : false
//            
            contentLabel.backgroundColor = UIColor.white
            
            // 根据数据展示或者隐藏
            guard let pic_urls = retweetViewModel?.status?.retweeted_status?.pic_urls else {
                return
            }
            
            bottom?.uninstall()
            
            if pic_urls.count > 0 {
                pictureView.isHidden = false
                
                
                self.snp_updateConstraints(closure: { (make) in
                    bottom =  make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                // 把数据传递过去
                pictureView.pic_urls = pic_urls
                
                // 改变
                
                
            } else {
                pictureView.isHidden = true
                
                
                // 高度还是会再的
                self.snp_updateConstraints(closure: { (make) in
                    bottom = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })

            }
            
        
        }
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        setupUI()
    }
    
    
    private func setupUI(){
        
        backgroundColor = UIColor.white//UIColor(white: 0.95, alpha: 1.0)
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            
        }
        
        
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(self.snp_left).offset(WBOrginalViewMargin)
            make.top.equalTo(contentLabel.snp_bottom)

        }
        
        
        
        // 注意: 自动设置汉高要告诉视图的底部在哪里
        self.snp_makeConstraints { (make) in
            bottom = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
       
    }
    
    
  private lazy var contentLabel: UILabel = UILabel(labelText: "微博内容", labelFont: 16, textColor: UIColor.black, align: NSTextAlignment.left)
   
    private lazy var pictureView : WBPictrueView = WBPictrueView()
    
}
